#include <iostream>
#include <unordered_map>
#include <vector>
#include <algorithm>

struct Record
{
    std::string name;
    std::unordered_map<std::string, int> score_map;
    int total_score;
};

std::string PROJ;

bool cmp1(Record rec1, Record rec2)
{
    if (rec1.score_map[PROJ] > rec2.score_map[PROJ])
    {
        return true;
    }
    else if (rec1.score_map[PROJ] == rec2.score_map[PROJ])
    {
        return rec1.name < rec2.name;
    }
    else
    {
        return false;
    }
}

bool cmp2(Record rec1, Record rec2)
{
    if (rec1.total_score > rec2.total_score)
    {
        return true;
    }
    else if (rec1.total_score == rec2.total_score)
    {
        return rec1.name < rec2.name;
    }
    else
        return false;
}

int main()
{
    int n, m;
    std::cin >> n >> m;
    std::cin.ignore();

    std::string proj;
    std::vector<std::string> proj_list;
    for (int i = 0; i < m; i++)
    {
        std::cin >> proj;
        proj_list.push_back(proj);
    }

    std::vector<Record> record_list;
    for (int i = 0; i < n; i++)
    {
        std::string name;
        std::cin >> name;

        Record rec;
        rec.name = name;
        rec.total_score = 0;

        for (int j = 0; j < m; j++)
        {
            int score;
            std::cin >> score;
            rec.total_score += score;
            rec.score_map[proj_list[j]] = score;
        }

        record_list.push_back(rec);
    }

    std::string query;
    std::cin >> query;

    int is_find = false;
    for (int i = 0; i < m; i++)
    {
        if (query == proj_list[i])
        {
            is_find = true;
            break;
        }
    }

    if (is_find)
    {
        PROJ = query;
        std::sort(record_list.begin(), record_list.end(), cmp1);
    }
    else
    {
        std::sort(record_list.begin(), record_list.end(), cmp2);
    }

    for (int i = 0; i < record_list.size() - 1; i++)
    {
        std::cout << record_list[i].name << " ";
    }
    std::cout << record_list.back().name << std::endl;

    return 0;
}